package top.zhangjianyong.blog.response;

import lombok.Data;
import top.zhangjianyong.blog.enums.RespCode;

/**
 * 接口响应类
 *
 * @author zhangjianyong
 * @since 2024/7/19 下午1:51
 */
@Data
public class ApiResponse {
  private Integer code;
  private String message;
  private Object data;

  public static ApiResponse success() {
    ApiResponse apiResponse = new ApiResponse();
    apiResponse.setCode(RespCode.SUCCESS.getCode());
    apiResponse.setMessage(RespCode.SUCCESS.getMsg());
    return apiResponse;
  }

  public static ApiResponse success(Object t) {
    ApiResponse success = success();
    success.setData(t);
    return success;
  }

  public static ApiResponse fail(Integer code, String message, Object data) {
    ApiResponse apiResponse = new ApiResponse();
    apiResponse.setCode(code);
    apiResponse.setMessage(message);
    apiResponse.setData(data);
    return apiResponse;
  }

  public static ApiResponse fail(Integer code, String message) {
    return fail(code, message, null);
  }

  public static ApiResponse fail(String message) {
    return fail(message, null);
  }

  public static ApiResponse fail(String message, Object date) {
    return fail(500, message, date);
  }

  public static ApiResponse fail(RespCode respCode) {
    return fail(respCode.getCode(), respCode.getMsg());
  }

  public static ApiResponse fail(RespCode respCode, Object data) {
    return fail(respCode.getCode(), respCode.getMsg(), data);
  }
}
